package com.iknow.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.location.Location;
import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.iknow.IKnow;
import com.iknow.app.IKnowSystemConfig;
import com.iknow.task.GenericTask;
import com.iknow.task.TaskAdapter;
import com.iknow.task.TaskListener;
import com.iknow.task.TaskParams;
import com.iknow.task.TaskResult;
import com.iknow.ui.model.FriendAdapter;
import com.iknow.util.Loger;
import com.iknow.view.widget.MyListView;
import com.iknow.xmpp.service.Friend;
import com.iknow.xmpp.service.aidl.IKnowUser;
import com.iknow.xmpp.service.aidl.IXmppConnection;
import com.iknow.xmpp.service.aidl.IXmppFacade;
import com.iknow.xmpp.service.aidl.IXmppFacade.Stub;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class NearByListActivity extends Activity
{
  private static final Intent SERVICE_INTENT = new Intent();
  private AbsListView.OnScrollListener ListViewOnScrollListener;
  private final String TAG = "NearByListActivity";
  private AdapterView.OnItemClickListener listItemClickListener;
  private FriendAdapter mAdapter;
  private Button mBackButton;
  private boolean mBinded;
  private List<Friend> mFriendList;
  private Button mGetMoreButton;
  protected TaskListener mGetMoreListener;
  private boolean mGetOnce;
  private boolean mLBS;
  private MyListView mList;
  protected View mListViewFooter;
  private Location mMyLocation;
  private String mNickName;
  private ProgressDialog mProgressDialog;
  private ServiceConnection mSvrConn;
  private FriendTask mTask;
  private TaskListener mTaskListener;
  private IXmppFacade mXmppFacade;

  static
  {
    SERVICE_INTENT.setComponent(new ComponentName("com.iknow", "com.iknow.xmpp.service.IKnowXmppService"));
  }

  public NearByListActivity()
  {
    this.mSvrConn = new XmppServiceConnection(this, null);
    this.listItemClickListener = new AdapterView.OnItemClickListener(this)
    {
      public void onItemClick(, View paramView, int paramInt, long paramLong)
      {
        Friend localFriend = NearByListActivity.access$0(this.this$0).getItem(paramInt);
        Intent localIntent = new Intent(this.this$0, FriendActivity.class);
        localIntent.putExtra("friend", localFriend);
        this.this$0.startActivity(localIntent);
      }
    };
    this.ListViewOnScrollListener = new AbsListView.OnScrollListener(this)
    {
      public void onScroll(, int paramInt1, int paramInt2, int paramInt3)
      {
      }

      public void onScrollStateChanged(, int paramInt)
      {
        switch (paramInt)
        {
        default:
        case 2:
        case 1:
        case 0:
        }
        while (true)
        {
          while (true)
          {
            while (true)
            {
              return;
              NearByListActivity.access$0(this.this$0).setScrolling(true);
            }
            NearByListActivity.access$0(this.this$0).setScrolling(true);
          }
          NearByListActivity.access$0(this.this$0).scrollIdle(paramAbsListView);
        }
      }
    };
    this.mGetMoreListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "getMore";
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        super.onPostExecute(paramGenericTask, paramTaskResult);
        if (paramTaskResult != TaskResult.OK)
          Toast.makeText(this.this$0, NearByListActivity.access$1(this.this$0).getErrorMessage(), 0).show();
        while (true)
        {
          NearByListActivity.access$3(this.this$0).setText("更多");
          NearByListActivity.access$0(this.this$0).notifyDataSetChanged();
          return;
          NearByListActivity.access$2(this.this$0);
        }
      }

      public void onProgressUpdate(, Object paramObject)
      {
      }
    };
    this.mTaskListener = new TaskAdapter(this)
    {
      public String getName()
      {
        return "FriendTask";
      }

      public void onCancelled()
      {
        NearByListActivity.access$7(this.this$0, null);
        NearByListActivity.access$8(this.this$0, null);
      }

      public void onPostExecute(, TaskResult paramTaskResult)
      {
        NearByListActivity.access$4(this.this$0).dismiss();
        NearByListActivity.access$5(this.this$0, null);
        if (paramTaskResult != TaskResult.OK)
          Toast.makeText(this.this$0, NearByListActivity.access$1(this.this$0).getErrorMessage(), 0).show();
        while (true)
        {
          return;
          NearByListActivity.access$2(this.this$0);
          if (NearByListActivity.access$0(this.this$0).getCount() > 0)
            this.this$0.mListViewFooter.setVisibility(0);
          NearByListActivity.access$0(this.this$0).sortByDistance();
          NearByListActivity.access$0(this.this$0).notifyDataSetChanged();
          NearByListActivity.access$6(this.this$0).clear();
        }
      }

      public void onPreExecute()
      {
        if (NearByListActivity.access$4(this.this$0) == null)
          NearByListActivity.access$5(this.this$0, new ProgressDialog(this.this$0));
        NearByListActivity.access$4(this.this$0).setMessage("正在加载同学列表，请稍后");
        NearByListActivity.access$4(this.this$0).show();
      }
    };
  }

  private void doNeedMore()
  {
    startGetFriendInfo();
  }

  private List<Friend> getFriendsByNick(int paramInt1, int paramInt2)
  {
    List localList1;
    List localList2;
    try
    {
      localList2 = this.mXmppFacade.createConnection().getIKnowUser().queryFriendsByNick(this.mNickName, paramInt1, paramInt2);
      localList1 = localList2;
      return localList1;
    }
    catch (Exception localException)
    {
      localException.printStackTrace();
      localList1 = null;
    }
  }

  private List<Friend> getNearByFriends(int paramInt1, int paramInt2)
  {
    List localList1;
    try
    {
      if ((this.mXmppFacade.createConnection() != null) && (this.mXmppFacade.createConnection().getIKnowUser() != null) && (this.mXmppFacade.createConnection().getIKnowUser().updateUserLocation(this.mMyLocation.getLongitude(), this.mMyLocation.getLatitude())))
      {
        List localList2 = this.mXmppFacade.createConnection().getIKnowUser().getNearByFriends(this.mMyLocation.getLongitude(), this.mMyLocation.getLatitude(), 0.5D, paramInt1, paramInt2);
        localList1 = localList2;
        return localList1;
      }
    }
    catch (Exception localException)
    {
      Loger.e("NearByListActivity", localException.getMessage());
      localList1 = null;
    }
  }

  private void readFriends()
  {
    Iterator localIterator = this.mFriendList.iterator();
    while (true)
    {
      Friend localFriend;
      do
      {
        if (!(localIterator.hasNext()))
          return;
        localFriend = (Friend)localIterator.next();
      }
      while (localFriend.getID().equalsIgnoreCase(IKnow.mSystemConfig.getString("account_username")));
      if (this.mLBS)
      {
        Location localLocation = new Location("iKnow");
        localLocation.setLatitude(localFriend.getLatitude());
        localLocation.setLongitude(localFriend.getLongitude());
        localFriend.setDistance(this.mMyLocation.distanceTo(localLocation));
      }
      this.mAdapter.addFriend(localFriend);
    }
  }

  private void setupListViewFooter()
  {
    this.mListViewFooter = getLayoutInflater().inflate(2130903105, null);
    this.mGetMoreButton = ((Button)this.mListViewFooter.findViewById(2131296480));
    this.mGetMoreButton.setOnClickListener(new View.OnClickListener(this)
    {
      public void onClick()
      {
        NearByListActivity.access$17(this.this$0);
      }
    });
    this.mList.addFooterView(this.mListViewFooter);
    this.mList.setAdapter(this.mAdapter);
    this.mListViewFooter.setVisibility(8);
  }

  private void startGetFriendInfo()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING));
    while (true)
    {
      return;
      this.mTask = new FriendTask(this, null);
      this.mTask.setListener(this.mTaskListener);
      TaskParams localTaskParams = new TaskParams();
      localTaskParams.put("offset", Integer.valueOf(this.mAdapter.getCount()));
      localTaskParams.put("length", Integer.valueOf(20));
      FriendTask localFriendTask = this.mTask;
      TaskParams[] arrayOfTaskParams = new TaskParams[1];
      arrayOfTaskParams[0] = localTaskParams;
      localFriendTask.execute(arrayOfTaskParams);
    }
  }

  protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    this.mAdapter = new FriendAdapter(this, getLayoutInflater());
    this.mLBS = getIntent().getBooleanExtra("lbs", false);
    this.mAdapter.setBShowLBS(this.mLBS);
    this.mAdapter.isShowFriendTags(true);
    if (this.mLBS)
    {
      this.mMyLocation = new Location("iKnow");
      this.mMyLocation.setLatitude(getIntent().getDoubleExtra("lat", 0D));
      this.mMyLocation.setLongitude(getIntent().getDoubleExtra("lon", 0D));
    }
    while (true)
    {
      requestWindowFeature(1);
      setContentView(2130903123);
      this.mFriendList = new ArrayList();
      this.mList = ((MyListView)findViewById(2131296466));
      this.mList.setOnItemClickListener(this.listItemClickListener);
      this.mList.setOnScrollListener(this.ListViewOnScrollListener);
      setupListViewFooter();
      this.mList.setAdapter(this.mAdapter);
      this.mBackButton = ((Button)findViewById(2131296342));
      this.mBackButton.setOnClickListener(new View.OnClickListener(this)
      {
        public void onClick()
        {
          this.this$0.finish();
        }
      });
      findViewById(2131296344).setVisibility(8);
      return;
      this.mNickName = getIntent().getStringExtra("nick");
    }
  }

  protected void onDestroy()
  {
    if ((this.mTask != null) && (this.mTask.getStatus() == AsyncTask.Status.RUNNING))
      this.mTask.cancel(true);
    if (this.mFriendList != null)
    {
      this.mFriendList.clear();
      this.mFriendList = null;
    }
    if (this.mAdapter != null)
    {
      this.mAdapter.clearFriend();
      this.mAdapter = null;
    }
    super.onDestroy();
  }

  protected void onPause()
  {
    if (this.mBinded)
    {
      unbindService(this.mSvrConn);
      this.mBinded = false;
    }
    super.onPause();
  }

  protected void onResume()
  {
    this.mBinded = bindService(SERVICE_INTENT, this.mSvrConn, 1);
    super.onResume();
  }

  private class FriendTask extends GenericTask
  {
    private String mErrorMessage;

    protected TaskResult _doInBackground()
    {
      TaskResult localTaskResult;
      TaskParams localTaskParams = paramArrayOfTaskParams[0];
      try
      {
        if ((NearByListActivity.access$9(this.this$0) == null) || (NearByListActivity.access$9(this.this$0).createConnection() == null) || (NearByListActivity.access$9(this.this$0).createConnection().getIKnowUser() == null))
        {
          this.mErrorMessage = "网络超时，稍后再试";
          localTaskResult = TaskResult.FAILED;
        }
        else if (NearByListActivity.access$10(this.this$0))
        {
          NearByListActivity.access$7(this.this$0, NearByListActivity.access$11(this.this$0, localTaskParams.getInt("offset"), localTaskParams.getInt("length")));
          if (NearByListActivity.access$6(this.this$0) == null)
          {
            this.mErrorMessage = "网络超时，稍后再试";
            localTaskResult = TaskResult.FAILED;
          }
        }
        else
        {
          NearByListActivity.access$7(this.this$0, NearByListActivity.access$12(this.this$0, localTaskParams.getInt("offset"), localTaskParams.getInt("length")));
        }
      }
      catch (Exception localException)
      {
        this.mErrorMessage = "网络超时，稍后再试";
        localTaskResult = TaskResult.FAILED;
        localTaskResult = TaskResult.OK;
        return localTaskResult;
      }
    }

    public String getErrorMessage()
    {
      return this.mErrorMessage;
    }
  }

  private class XmppServiceConnection
  implements ServiceConnection
  {
    public void onServiceConnected(, IBinder paramIBinder)
    {
      NearByListActivity.access$13(this.this$0, IXmppFacade.Stub.asInterface(paramIBinder));
      if (!(NearByListActivity.access$14(this.this$0)))
        NearByListActivity.access$15(this.this$0);
      NearByListActivity.access$16(this.this$0, true);
    }

    public void onServiceDisconnected()
    {
      NearByListActivity.access$13(this.this$0, null);
    }
  }
}